package com.ltkj.shop.service.impl;

import com.ltkj.common.core.exception.ServiceException;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.shop.domain.ShopGoodsCenters;
import com.ltkj.shop.mapper.ShopGoodsCentersBizMapper;
import com.ltkj.shop.mapper.ShopGoodsCentersMapper;
import com.ltkj.shop.mapper.ShopStoreCentersBizMapper;
import com.ltkj.shop.service.IShopGoodsCentersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品自提点Service业务层处理
 * 
 * @author xupf
 * @date 2023-08-29
 */
@Service
public class ShopGoodsCentersServiceImpl implements IShopGoodsCentersService 
{
    @Autowired
    private ShopGoodsCentersMapper shopGoodsCentersMapper;
    @Autowired
    private ShopGoodsCentersBizMapper shopGoodsCentersBizMapper;
    @Autowired
    private ShopStoreCentersBizMapper shopStoreCentersBizMapper;

    /**
     * 查询商品自提点
     * 
     * @param id 商品自提点主键
     * @return 商品自提点
     */
    @Override
    public ShopGoodsCenters selectShopGoodsCentersById(Long id)
    {
        return shopGoodsCentersMapper.selectShopGoodsCentersById(id);
    }

    /**
     * 查询商品自提点列表
     * 
     * @param shopGoodsCenters 商品自提点
     * @return 商品自提点
     */
    @Override
    public List<ShopGoodsCenters> selectShopGoodsCentersList(ShopGoodsCenters shopGoodsCenters)
    {
        shopGoodsCenters.setIsDel("0");
        return shopGoodsCentersMapper.selectShopGoodsCentersList(shopGoodsCenters);
    }

    /**
     * 新增商品自提点
     * 
     * @param shopGoodsCenters 商品自提点
     * @return 结果
     */
    @Override
    public int insertShopGoodsCenters(ShopGoodsCenters shopGoodsCenters)
    {
        shopGoodsCenters.setCreateTime(DateUtils.getNowDate());
        shopGoodsCenters.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        shopGoodsCenters.setIsDel("0");
        return shopGoodsCentersMapper.insertShopGoodsCenters(shopGoodsCenters);
    }

    /**
     * 修改商品自提点
     * 
     * @param shopGoodsCenters 商品自提点
     * @return 结果
     */
    @Override
    public int updateShopGoodsCenters(ShopGoodsCenters shopGoodsCenters)
    {
        shopGoodsCenters.setUpdateTime(DateUtils.getNowDate());
        shopGoodsCenters.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return shopGoodsCentersMapper.updateShopGoodsCenters(shopGoodsCenters);
    }

    /**
     * 批量删除商品自提点
     * 
     * @param ids 需要删除的商品自提点主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsCentersByIds(Long[] ids)
    {
        for (Long id : ids) {
            if (countStoreByCentersId(id) > 0)
            {
                throw new ServiceException("已有商户关联,不能删除");
            }
        }
        return shopGoodsCentersBizMapper.deleteShopGoodsCentersByIds(ids);
    }

    /**
     * 根据自提点ID查询关联关系
     * @param id 自提点ID
     * @return
     */
    private int countStoreByCentersId(Long id) {
        return shopStoreCentersBizMapper.countByCentersId(id);
    }

    /**
     * 删除商品自提点信息
     * 
     * @param id 商品自提点主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsCentersById(Long id)
    {
        if (countStoreByCentersId(id) > 0)
        {
            throw new ServiceException("已有商户关联,不能删除");
        }
        return shopGoodsCentersBizMapper.deleteShopGoodsCentersById(id);
    }
}
